package com.choudou5.web;

import cn.hutool.core.io.IoUtil;
import com.choudou5.utils.JsonUtil;
import com.choudou5.utils.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * XXX
 * @author xuhaowende@sina.cn
 * @time 2019-08-15
 */
public class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    protected <T> T buildBo(HttpServletRequest request, Class<T> clssz){
        if(request.getContentLength() < 0) {
            LogUtil.debug("请求无参数");
            return null;
        }
        try {
            String requestBody = IoUtil.read(request.getInputStream(), "utf-8");
            LogUtil.debug("request body = ", requestBody);
            return JsonUtil.toJavaObj(requestBody, clssz);
        } catch (IOException e) {
            logger.error("消息转换失败", e);
        }
        return null;
    }

}
